package com.xbongbong.saas.help;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.domain.entity.ext.ConditionsEntityExt;
import com.xbongbong.paas.elasticsearch.pojo.XbbAggregatedPage;
import com.xbongbong.paas.elasticsearch.util.EsUtil;
import com.xbongbong.paas.elasticsearch.util.XbbElasticsearchRestTemplate;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.saas.domain.entity.ext.ProductEntityExt;
import com.xbongbong.saas.enums.business.ProductEnum;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.elasticsearch.index.query.QueryBuilders.termsQuery;

/**
 * @author 吴峰
 * @date 2022/05/13 14:57
 */
@Component
public class SaasLinkProductHelp {

    @Resource
    private XbbElasticsearchRestTemplate xbbElasticsearchRestTemplate;

    /**
     * com.xbongbong.saas.help.SaasListHelp#selectProductCondition(java.util.List, java.lang.String)
     * 产品查询，sku查询带上spuId,兼容老数据
     * @param conditions
     * @param corpid
     */
    public void selectProductCondition(List<ConditionsEntityExt> conditions, String corpid) throws XbbException {
        Set<Object> skuIdSet = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        List<ConditionsEntityExt> productConditionList = new ArrayList<>();
        for (ConditionsEntityExt entityExt : conditions){
            if (Objects.equals(entityExt.getFieldType(), FieldTypeEnum.SELECT_PRODUCT.getType())){
                if (!Objects.isNull(entityExt.getValue()) && !entityExt.getValue().isEmpty()){
                    skuIdSet.addAll(entityExt.getValue());
                    productConditionList.add(entityExt);
                }
            }
        }
        if (productConditionList.isEmpty()){
            return;
        }
        if (skuIdSet.isEmpty()) {
            return;
        }
        //查询sku数据，获得spuId
        BoolQueryBuilder boolQueryBuilder = boolQuery();
        boolQueryBuilder.filter(termQuery("corpid.keyword",corpid));
        boolQueryBuilder.filter(termsQuery(FieldTypeEnum.DATAID.getAlias(),skuIdSet));
        SearchRequest searchRequest = EsUtil.simplePackageForQueryForPage(IndexTypeEnum.IDX_SAAS_PRODUCT,boolQueryBuilder, Arrays.asList(ProductEnum.PARENT_ID.getAttr(),FieldTypeEnum.DATAID.getAlias()), 0, skuIdSet.size());
        XbbAggregatedPage<ProductEntityExt> result = xbbElasticsearchRestTemplate.queryForPages(searchRequest,ProductEntityExt.class);
        List<ProductEntityExt> productList = result.getContent();
        //skuId,spuId
        Map<Long,Long> idMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (PaasFormDataEntity entity : productList){
            idMap.put(entity.getDataId(),entity.getData().getLong(ProductEnum.PARENT_ID.getAttr()));
        }
        //把spuId冗余回来
        for (ConditionsEntityExt entityExt : productConditionList){
            List<Object> value = entityExt.getValue();
            Set<Object> spuIdSet = new HashSet<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
            for (Object obj : value){
                Long skuId = Long.parseLong(obj.toString()) ;
                if (idMap.containsKey(skuId)){
                    spuIdSet.add(idMap.get(skuId));
                }
            }
            value.addAll(spuIdSet);
        }
    }
}
